/*
 * The MIT License
 *
 * Copyright 2011 Roman Yakovenko <erelus@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.stego.alg;

import com.stego.core.BitOperations;
import com.stego.core.Transformer;
import com.stego.image.Image;
import com.stego.image.Pixel;
import com.stego.transform.Wavelet;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class WaveletAlg implements Transformer {

    private Wavelet wavelet;
    private File file;

    public WaveletAlg(File file) {
        this.file = file;
        this.wavelet = new Wavelet();
    }

    @Override
    public Pixel[][] hideData(byte[] data) throws IOException {
        Image image = new Image(this.file);
        Pixel[][] pixels = image.getPixels();

        Pixel[][] smallPixels = this.wavelet.getLittleCopy(pixels);
        smallPixels = this.hide(smallPixels, data);
        Pixel[][] rowsPixels = this.wavelet.getRowsTransform(pixels);
        Pixel[][] colsPixels = this.wavelet.getColsTransform(pixels);
        Pixel[][] diagPixels = this.wavelet.getDiagonalsTransform(pixels);

        Pixel[][] resultPixels = this.wavelet.getInversedTransform(smallPixels, rowsPixels, colsPixels, diagPixels);

        return resultPixels;
    }

    protected Pixel[][] hide(Pixel[][] container, byte[] data) {

        int dataOffset = 0; //смещение в битах в массиве данных.
        for (int i = 0; i < container.length; i++) {
            for (int j = 0; j < container[i].length; j++) {
                int currentBlock = (dataOffset) / 8; // текущий элемент массива данных.

                int red = container[i][j].getRed();

                byte bitToHide;
                if (currentBlock < data.length) {
                    int currentBit = (dataOffset) % 8;
                    bitToHide = BitOperations.getBit(currentBit, data[currentBlock]);
                } else {
                    bitToHide = 0;
                }
                red = (red & 0xFE) | bitToHide;
                container[i][j].setRed((byte) red);
                dataOffset++;
            }
        }
        return container;
    }

    @Override
    public byte[] getData() throws IOException {
        Image image = new Image(this.file);
        Pixel[][] pixels = image.getPixels();
        Pixel[][] smallPixels = this.wavelet.getLittleCopy(pixels);
        byte[] result = this.getHidden(smallPixels);
        return result;
    }

    protected byte[] getHidden(Pixel[][] container) {
        int size = container.length * container[0].length / 8 + 1;
        byte[] result = new byte[size];
        Arrays.fill(result, (byte) 0);
        int dataOffset = 0; //смещение в битах в массиве данных.
        for (int i = 0; i < container.length; i++) {
            for (int j = 0; j < container[i].length; j++) {
                int block = dataOffset / 8;
                int pixel = dataOffset % 8;
                int red = container[i][j].getRed();
                int bitToOpen = BitOperations.getBit(LSB, (byte) red);
                result[block] = (byte) (result[block] | (bitToOpen << pixel));
                dataOffset++;
            }
        }
        return result;
    }


}
